﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Xml;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using Bricks.Site.Models;
using System.Configuration;
using System.ComponentModel.Composition.Hosting;
using Bricks.Business.Service;
using Bricks.Component.Data;
using Bricks.Site.Models.DBViewModel.Contract;
using Bricks.Business.Models.ProjectContract;
using Bricks.Business.Models.ScheduleManagement;

namespace Bricks.Site.Web.CommonUtils
{
    public class ScheduleManagementUtils
    {
        /// <summary>
        /// 构造主界面按钮，设置主界面model
        /// </summary>
        /// <param name="contractFilterView">标段筛选视图：用于主界面的判断</param>
        /// <param name="userName">登录人用户编码</param>
        /// <param name="menuActions">功能点列表</param>
        /// <returns></returns>
        public static string ConstructToolsAndSetView(ContractFilterView contractFilterView, string userName, List<UIFunctionView> menuActions, string moduleCode)
        {
            var container = HttpContext.Current.Application["Container"] as CompositionContainer;
            var userInfoService = container.GetExportedValueOrDefault<IUserInfoService>();
            var userOrgService = container.GetExportedValueOrDefault<IUserOrgService>();
            var orgInfoService = container.GetExportedValueOrDefault<IOrgInfoService>();
            StringBuilder sb = new StringBuilder();
            var orgs = (from u in userInfoService.Users
                        join uo in userOrgService.UserOrgs on u.ID equals uo.UserID
                        join o in orgInfoService.Orgs on uo.OrgID equals o.ID
                        where u.UserName == userName
                        select o).ToList();

            string requestDepartmentList = string.Empty;
            switch (moduleCode)
            {
                case "TotalMaintenanceList":
                    requestDepartmentList = ConfigurationManager.AppSettings["RequestDepartmentListForTotalMaintenance"] as string;
                    break;
                case "YearPlanList":
                    requestDepartmentList = ConfigurationManager.AppSettings["RequestDepartmentListForYearPlan"] as string;
                    break;
                case "MonthPlanList":
                    requestDepartmentList = ConfigurationManager.AppSettings["RequestDepartmentListForMonthPlan"] as string;
                    break;
                case "StageScheduleList":
                    requestDepartmentList = ConfigurationManager.AppSettings["RequestDepartmentListForStageSchedule"] as string;
                    break;
                case "EngineeringImageScheduleList":
                    requestDepartmentList = ConfigurationManager.AppSettings["RequestDepartmentListForEngineeringImageSchedule"] as string;
                    break;
            }

            if (orgs.Count() > 0 && requestDepartmentList != string.Empty)
            {
                foreach (var orgInfo in orgs)
                {
                    if (FlowFilterAndRequestUtils.IsHasOrgCodeAndUserNameInStr(requestDepartmentList, orgInfo.OrgCode, userName))
                    {
                        contractFilterView.UserType = "0";
                        string sql = "select * from V_ContractInfoes where CorrespondenceUnit ='" + orgInfo.ID + "' ORDER BY LevelCode";
                        var lstContractInfo = DbSqlHelper<VMContractInfo>.SqlQueryCommon(sql);
                        if (lstContractInfo.Count > 0)
                        {
                            var projectInfoService = container.GetExportedValueOrDefault<IProjectInfoService>();
                            Guid projectID = lstContractInfo[0].PID;
                            ProjectInfo projectInfo = projectInfoService.ProjectInfoes.SingleOrDefault(p => p.ID == projectID);
                            if (projectInfo != null)
                            {
                                contractFilterView.ProjectID = projectInfo.ID;
                                contractFilterView.ProjectName = projectInfo.ProjectName;
                                contractFilterView.ContractType = lstContractInfo[0].ContractType;
                                contractFilterView.ContractID = lstContractInfo[0].ID;
                                contractFilterView.ContractName = lstContractInfo[0].ContractName;
                            }
                        }

                        if (contractFilterView.ProjectID == null || string.IsNullOrEmpty(contractFilterView.ContractType) || contractFilterView.ContractID == null)
                        {
                            SetAllDisabled(menuActions, sb);
                        }
                        else
                        {
                            if (moduleCode == "TotalMaintenanceList")
                            {
                                ConstructToolsForTotalMaintenance(contractFilterView, menuActions, sb);
                            }
                            else
                            {
                                foreach (UIFunctionView item in menuActions)
                                {
                                    sb.Append(ToolButton(item));
                                }
                            }
                        }

                        break;
                    }
                }
            }
            return sb.ToString();
        }

        private static void SetAllDisabled(List<UIFunctionView> menuActions, StringBuilder sb)
        {
            foreach (UIFunctionView item in menuActions)
            {
                sb.Append(ToolButton(item, "disabled='false'"));
            }
        }

        public static string ToolButton(UIFunctionView item, string disabled = "")
        {
            return "<a href='javascript:void(0)' id='btn" + item.ActionCode + "' class='easyui-linkbutton' onclick='" + item.MenuCode + "." + item.ActionCode + "()' data-options='plain:true,iconCls:\"" + item.Icon + "\"'" + disabled + ">" + item.ActionName + "</a>";
        }

        private static void ConstructToolsForTotalMaintenance(ContractFilterView contractFilterView, List<UIFunctionView> menuActions, StringBuilder sb)
        {
            var container = HttpContext.Current.Application["Container"] as CompositionContainer;
            var totalMaintenanceService = container.GetExportedValueOrDefault<ITotalMaintenanceService>();
            TotalMaintenance totalMaintenance = totalMaintenanceService.TotalMaintenances.SingleOrDefault(t => t.ProjectID == contractFilterView.ProjectID && t.ContractType == contractFilterView.ContractType && t.ContractID == contractFilterView.ContractID);
            foreach (UIFunctionView item in menuActions)
            {
                if (item.ActionCode == "Add")
                {
                    if (totalMaintenance != null)
                    {
                        sb.Append(ToolButton(item, "disabled='false'"));
                    }
                    else
                    {
                        sb.Append(ToolButton(item));
                    }

                }

                if (item.ActionCode == "Revise")
                {
                    if (totalMaintenance != null && totalMaintenance.HandleState == "2")
                    {
                        sb.Append(ToolButton(item));
                    }
                    else
                    {
                        sb.Append(ToolButton(item, "disabled='false'"));
                    }

                }

                if (item.ActionCode == "Delete")
                {
                    if (totalMaintenance != null)
                    {
                        sb.Append(ToolButton(item));
                    }
                    else
                    {
                        sb.Append(ToolButton(item, "disabled='false'"));
                    }

                }
            }

        }
    }
}
